package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.dto.RoleDto;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.domain.Role;
import com.bjsxt.service.RoleService;
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public DataGridView listRolePage(RoleDto roleDto) {
        Page<Role> page = new Page<>(roleDto.getPageNum(),roleDto.getPageSize());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        //根据角色名称模糊查询
        qw.like(StringUtils.isNotBlank(roleDto.getRoleName()),Role.COL_ROLE_NAME,roleDto.getRoleName());
        //根据角色权限编码模糊查询
        qw.like(StringUtils.isNotBlank(roleDto.getRoleCode()),Role.COL_ROLE_CODE,roleDto.getRoleCode());
        //根据角色状态进行查询
        qw.eq(StringUtils.isNotBlank(roleDto.getStatus()),Role.COL_STATUS,roleDto.getStatus());
        // 根据角色创建时间进行查询
        qw.ge(null!=roleDto.getBeginTime(),Role.COL_CREATE_TIME,roleDto.getBeginTime());
        qw.le(null!=roleDto.getEndTime(),Role.COL_CREATE_TIME,roleDto.getEndTime());
        // 根据排序码进行排序
        qw.orderByAsc(Role.COL_ROLE_SORT);
        roleMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public List<Role> listAllRoles() {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq(Role.COL_STATUS, Constants.STATUS_TRUE);
        qw.orderByAsc(Role.COL_ROLE_SORT);
        return roleMapper.selectList(qw);
    }

    @Override
    public Role getOne(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public int addRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto,role);
        role.setCreateBy(roleDto.getSimpleUser().getUserName());
        role.setCreateTime(DateUtil.date());
        return roleMapper.insert(role);
    }

    @Override
    public int updateRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto,role);
        role.setUpdateBy(roleDto.getSimpleUser().getUserName());
        return roleMapper.updateById(role);
    }

    @Override
    public int deleteRoleByIds(Long[] roleIds) {
        List<Long> ids = Arrays.asList(roleIds);
        if (null!=ids&&!ids.isEmpty()){
            //删除sys_role_menu与当前角色id有关的信息
            roleMapper.deleteRoleMenuByRoleIds(ids);
            //删除sys_role_user与当前角色id有关的信息
            roleMapper.deleteRoleUserByRoleIds(ids);
            return roleMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    @Override
    public void saveRoleMenu(Long roleId, Long[] menuIds) {
        // 根据角色id删除sys_role_menu的数据
        roleMapper.deleteRoleMenuByRoleIds(Arrays.asList(roleId));
        for (Long menuId : menuIds) {
            roleMapper.saveRoleMenu(roleId,menuId);
        }
    }

    /**
     * 根据用户id获取角色id列表
     * @param userId
     * @return
     */
    @Override
    public List<Long> getRoleIdsByUserId(Long userId) {
        if (null==userId){
            return null;
        }
        // 根据用户id查询角色id列表
        return roleMapper.selectRoleIdsByUserId(userId);
    }

    /**
     * 保存用户和角色之间的关系
     * @param userId
     * @param roleIds
     */
    @Override
    public void saveRoleUser(Long userId, Long[] roleIds) {
        //根据用户id删除sys_role_user里面的数据
        roleMapper.deleteRoleUserByUserIds(Arrays.asList(userId));
        for (Long roleId : roleIds) {
            // 保存
            roleMapper.saveRoleUser(userId,roleId);
        }
    }
}

